﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MySql.Data.MySqlClient;
using SIA.DataBase;

namespace SIA.Panels.Allarms
{
    public partial class StoricoAllarmi : Form
    {
        private IDBArchive _dBArchive;
        public StoricoAllarmi(IDBArchive dBArchive)
        {
            InitializeComponent();

            this._dBArchive = dBArchive;

            dtFine.Value = DateTime.Now;
            dtInizio.Value = DateTime.Now;
            updateTable();
            updateTable("WHERE DataOraON<='" + dtFine.Value.ToString("yyyy-MM-dd") + " 23:59:59" + "' AND (DataOraOFF>='" + dtInizio.Value.ToString("yyyy-MM-dd") + " 00:00:00" + "' OR DataOraACK>='" + dtInizio.Value.ToString("yyyy-MM-dd") + " 00:00:00" + "')");

            cbNomeAllarme.TextChanged += new EventHandler(cbNomeAllarme_TextChanged);
            cbGruppo.TextChanged += new EventHandler(cbGruppo_TextChanged);
            dtInizio.TextChanged += new EventHandler(dtInizio_ValueChanged);
            dtFine.ValueChanged += new EventHandler(dtFine_ValueChanged);
            
        }

        void dtInizio_ValueChanged(object sender, EventArgs e)
        {
            if (dtInizio.Value > dtFine.Value)
                dtFine.Value = dtInizio.Value;
            updateTable("WHERE DataOraON<='" + dtFine.Value.ToString("yyyy-MM-dd") + " 23:59:59" + "' AND (DataOraOFF>='" + dtInizio.Value.ToString("yyyy-MM-dd") + " 00:00:00" + "' OR DataOraACK>='" + dtInizio.Value.ToString("yyyy-MM-dd") + " 00:00:00" + "')");
        }

        void dtFine_ValueChanged(object sender, EventArgs e)
        {
            if (dtFine.Value < dtInizio.Value)
                dtInizio.Value = dtFine.Value;
            updateTable("WHERE DataOraON<='" + dtFine.Value.ToString("yyyy-MM-dd") + " 23:59:59" + "' AND (DataOraOFF>='" + dtInizio.Value.ToString("yyyy-MM-dd") + " 00:00:00" + "' OR DataOraACK>='" + dtInizio.Value.ToString("yyyy-MM-dd") + " 00:00:00" + "')");
        }     

        void cbGruppo_TextChanged(object sender, EventArgs e)
        {
            cbNomeAllarme.SelectedIndex = cbNomeAllarme.Items.Count - 1;
            if (cbGruppo.Text == "--------" || cbGruppo.Text=="")
                       updateTable("");
            else
                updateTable("WHERE Gruppo='" + cbGruppo.Text+"'" + " AND DataOraON<='" + dtFine.Value.ToString("yyyy-MM-dd") + " 23:59:59" + "' AND (DataOraOFF>='" + dtInizio.Value.ToString("yyyy-MM-dd") + " 00:00:00" + "' OR DataOraACK>='" + dtInizio.Value.ToString("yyyy-MM-dd") + " 00:00:00" + "')");
        }

        void cbNomeAllarme_TextChanged(object sender, EventArgs e)
        {
            cbGruppo.SelectedIndex = cbGruppo.Items.Count - 1;
            if (cbNomeAllarme.Text == "--------" || cbNomeAllarme.Text == "")
                updateTable("");
            else
                updateTable("WHERE NomeAllarme='" + cbNomeAllarme.Text + "'" + " AND DataOraON<='" + dtFine.Value.ToString("yyyy-MM-dd") + " 23:59:59" + "' AND (DataOraOFF>='" + dtInizio.Value.ToString("yyyy-MM-dd") + " 00:00:00" + "' OR DataOraACK>='" + dtInizio.Value.ToString("yyyy-MM-dd") + " 00:00:00" + "')");

                
        }

        private void updateTable(String filtroSql)
        {
            dgStoricoAllarmi.DataSource = _dBArchive.Select("SELECT NomeAllarme as 'Nome Allarme', Gruppo, DataOraON as 'Data e Ora ON', DataOraOFF as 'Data e Ora OFF',DataOraACK as 'Data e Ora ACK' , Comment  FROM "
                    + _dBArchive.StoricAllarmsTable + " " + filtroSql);
        }

        private void updateTable()
        {
            dgStoricoAllarmi.DataSource = _dBArchive.Select("SELECT NomeAllarme as 'Nome Allarme', Gruppo, DataOraON as 'Data e Ora ON', DataOraOFF as 'Data e Ora OFF',DataOraACK as 'Data e Ora ACK', Comment FROM "
                    + _dBArchive.StoricAllarmsTable);
        
            //codice per formattare alcuni campi della datagridview
            dgStoricoAllarmi.Columns["Nome Allarme"].MinimumWidth = 200;
            dgStoricoAllarmi.Columns["Gruppo"].MinimumWidth = 150;
            dgStoricoAllarmi.Columns["Data e Ora ON"].MinimumWidth = 120;
            dgStoricoAllarmi.Columns["Data e Ora OFF"].MinimumWidth = 120;
            dgStoricoAllarmi.Columns["Data e Ora ACK"].MinimumWidth = 120;
            dgStoricoAllarmi.Columns["Comment"].MinimumWidth = 300;
            

            DataTable dt = _dBArchive.Select("SELECT DISTINCT(NomeAllarme) as 'Nome Allarme' FROM " + _dBArchive.StoricAllarmsTable + " as DISTINCT_NomeAllarme");
            dt.Rows.Add("--------");
            cbNomeAllarme.DataSource = dt;
            cbNomeAllarme.DisplayMember = "Nome Allarme";
            cbNomeAllarme.SelectedIndex = cbNomeAllarme.Items.Count - 1;

            dt = _dBArchive.Select("SELECT DISTINCT(Gruppo) as 'Gruppo' FROM " + _dBArchive.StoricAllarmsTable + " as DISTINCT_Gruppo");
            dt.Rows.Add("--------");
            cbGruppo.DataSource = dt;
            cbGruppo.DisplayMember = "Gruppo";
            cbGruppo.SelectedIndex = cbGruppo.Items.Count - 1;
        }

        private void btChiudi_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            updateTable("");
        }

    }
}
